package com.realbrand.net.realbrandpay.ui.activity.mall;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.base.FirstActivity;
import com.realbrand.net.realbrandpay.ui.activity.ADSActivity;
import com.realbrand.net.realbrandpay.ui.activity.login.LoginActivity;
import com.realbrand.net.realbrandpay.ui.activity.shopcar.ShopCarActivity;
import com.realbrand.net.realbrandpay.ui.activity.wallet.WalletActivity;
import com.realbrand.net.realbrandpay.ui.activity.wallet.set.WalletLogoutActivity;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.app.CaptureActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/16.
 */

public class MallActivity extends FirstActivity {

    @BindView(R.id.mall_webview)
    WebView mallWebview;
    @BindView(R.id.mall_tv_scan)
    TextView mallTvScan;
    @BindView(R.id.mall_tv_shopcar)
    TextView mallTvShopcar;
    @BindView(R.id.mall_tv_my)
    TextView mallTvMy;


    private String url = "h5mall.realbrand.net";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        SharedPreferences userMassage = getSharedPreferences("userMassage", MODE_PRIVATE);
        String account = userMassage.getString("account", "zxy123");
        String credentials = userMassage.getString("credentials", "");
        mallWebview.loadUrl("http://h5mall.realbrand.net" + "?" + "UserAccount=" + account + "&" + "LoginUser=" + account + "&" + "CREDENTIALS=" + credentials);
//      mallWebview.loadUrl("http://h5mall.realbrand.net" + "?" + "UserAccount=" + account);
        WebSettings webSettings = mallWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许使用js
        //取得缓存路径
        String path =getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath();
        //设置路径
        webSettings.setDatabasePath(path);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        //设置缓存
        webSettings.setAppCacheEnabled(true);
        mallWebview.requestFocus();
        setOnClick();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.mall_layout;
    }

    @Override
    protected void setOnClick() {
        mallTvShopcar.setOnClickListener(this);
        mallTvScan.setOnClickListener(this);
        mallTvMy.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CookieSyncManager.createInstance(this);
        CookieManager instance = CookieManager.getInstance();
        instance.removeAllCookie();
        CookieSyncManager.getInstance().sync();
        mallWebview.setWebChromeClient(null);
        mallWebview.setWebViewClient(null);
        mallWebview.getSettings().setJavaScriptEnabled(false);
        mallWebview.clearCache(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mall_tv_my:
                if (LoginActivity.isLogin) {
                    startActivity(new Intent(this, WalletActivity.class));
                    finish();
                } else {
                    startActivity(new Intent(this, WalletLogoutActivity.class));
                    finish();
                }

                break;
            case R.id.mall_tv_scan:
                ShopCarActivity.mBoolean = true;
                Intent intent = new Intent(this, CaptureActivity.class);
                Bundle bundle_scan = new Bundle();
                bundle_scan.putString(Static.BundleKey, ADSActivity.VALUE_ADS);
                intent.putExtras(bundle_scan);
                startActivity(intent);
                break;
            case R.id.mall_tv_shopcar:
                startActivity(new Intent(this, ShopCarActivity.class));
                finish();
                break;
        }
    }
}
